package com.qf.controller;

import com.qf.entity.*;
import com.qf.service.AdminService;
import com.qf.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@Controller
@RequestMapping("/admin/")
public class AdminController {
    @Autowired
    @Resource
   private AdminService adminService;

    @PostMapping("addStudent.do")
    private R addStudent(Student student) {
        return adminService.addStudetn(student);
    }

    @PostMapping("addTeacher.do")
    private R addTeacher(Teacher teacher) {
        return adminService.addTeacher(teacher);
    }
    //发布评教
    @RequestMapping("addComment.do")
    @ResponseBody
    public R addComment(String tnumber){
      return adminService.addComment(tnumber);
    }
    // 补考
    @RequestMapping("addExamtoo.do")
    @ResponseBody
    public R addExamtoo(Examtoo examtoo){

        return adminService.addExamtoo(examtoo);
    }

//J 录入考试
    @RequestMapping("addExam.do")
    @ResponseBody
    public R addExam(ExamSchedule schedule) {
        return adminService.addExam(schedule);
    }
//J 为考试安排教室 随机
    @RequestMapping("randomClassroom.do")
    @ResponseBody
    public R randomExam(int range) {
        return adminService.randomClassroom(range);
    }
//J 为考试安排监考老师
    @RequestMapping("randomTeacher.do")
    @ResponseBody
    public R randomTeacher(int range) {
        return adminService.randomTeacher(range);
    }
//J 为学生分班
    @RequestMapping("randomClass.do")
    @ResponseBody
    public R randomClass(int fromStu, int fromCls) {
        return adminService.randomClass(fromStu, fromCls);
    }

    @PostMapping("addCourse.do")
    private R addCourse(Course course) {
        return adminService.addCourse(course);
    }


    //学生信息表  k
    @RequestMapping("listStudent.do")
    @ResponseBody
    public R listStudent() {
        return new R(0,"success", adminService.listStudent());
    }


    //查询学生个人信息  k
    @RequestMapping("queryStudentById.do")
    @ResponseBody
    public R  queryStudentById(int id) {
        Student student = adminService.queryStudentById(id) ;
        return new R(0, "success", student) ;

    }

    //修改学生  k
    @RequestMapping("updateStudentById.do")
    @ResponseBody
    public R updateStudentById(Student student) {
        adminService.updateStudentById(student);
        R ret = adminService.updateStudentById(student);
        return ret;
    }

    //删除学生  k
    @RequestMapping("deleteStudent.do")
    public void deleteStudent(int id, HttpServletResponse resp) throws IOException {
        adminService.deleteStudent(id);
        boolean ret = adminService.deleteStudent(id);
        resp.setContentType("text/html;charset=utf-8");
        if (ret) {
            resp.getOutputStream().print("<script>alert('----no-----');window.location.href='/listStudent.html'</script>");
        } else {
            resp.getOutputStream().print("<script>alert('----ok----');window.location.href='/listStudent.html'</script>");
        }
    }

    @RequestMapping("queryTeacherById.do")
    @ResponseBody
    public R queryTeacherById(int id) {
        Teacher teacher = adminService.queryTeacherById(id) ;
        return new R(0, "success", teacher);
    }


    //老师信息表
    @RequestMapping("listTeacher.do")
    @ResponseBody
    public R listTeacher() {
        return new R(0, "success", adminService.listTeacher());
    }

    //修改教师信息 k
    @RequestMapping("updateTeacher.do")
    @ResponseBody
    public R updateTeacherById(Teacher teacher, HttpServletResponse resp) throws IOException {
        adminService.updateTeacherById(teacher);
        R ret = adminService.updateTeacherById(teacher);
        return ret;
    }

    //删除教师  k
    @RequestMapping("deleteTeacher.do")
    public void deleteTeacher(int id, HttpServletResponse resp) throws  IOException {
        adminService.deleteTeacher(id);
        boolean ret = adminService.deleteTeacher(id);
        resp.setContentType("text/html;charset=utf-8");
        if (ret) {
            resp.getOutputStream().print("<script>alert('----no-----');window.location.href='/listTeacher.html'</script>");
        } else {
            resp.getOutputStream().print("<script>alert('----ok----');window.location.href='/listTeacher.html'</script>");
        }
    }


    //管理员查看课程信息表 k
    @RequestMapping("listCourse.do")
    @ResponseBody
    public R listCourse() {
        return new R(0, "success", adminService.listCourse());
    }

    @RequestMapping("queryCourseById.do")
    @ResponseBody
    public R queryCourseById(int id) {
        Course course = adminService.queryCourseById(id) ;
        return new R(0, "success", course);
    }

    //修改课程信息  k
    @RequestMapping("updateCourse.do")
    @ResponseBody
    public R updateCourseById(Course course) {
        adminService.updateCourseById(course);
        R ret = adminService.updateCourseById(course);
        return ret;
    }

    //删除课程  k
    @RequestMapping("deleteCourse.do")
    public void deleteCourse(int id, HttpServletResponse resp) throws IOException {
        adminService.deleteCourse(id);
        boolean ret = adminService.deleteCourse(id);
        resp.setContentType("text/html;charset=utf-8");
        if (ret) {
            resp.getOutputStream().print("<script>alert('----no-----');window.location.href='/listCourse.html'</script>");
        } else {
            resp.getOutputStream().print("<script>alert('----ok----');window.location.href='/listCourse.html'</script>");
        }
    }
}

